home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mm / mm-0.90 / dt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-18  |  5.0 KB  |  210 lines

  1. /*
  2.  * Copyright (c) 1986, 1990 by The Trustees of Columbia University in
  3.  * the City of New York.  Permission is granted to any individual or
  4.  * institution to use, copy, or redistribute this software so long as it
  5.  * is not sold for profit, provided this copyright notice is retained.
  6.  */
  7.  
  8. #ifndef lint
  9. static char *rcsid = "$Header: /f/src2/encore.bin/cucca/mm/tarring-it-up/RCS/dt.c,v 2.1 90/10/04 18:24:01 melissa Exp $";
  10. #endif
  11.  
  12. #include "mm.h"
  13. #include "ccmd.h"
  14.  
  15. #ifdef howmany
  16. #undef howmany
  17. #endif
  18.  
  19. static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
  20.                   "Aug", "Sep", "Oct", "Nov", "Dec" };
  21. static int monthlens[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
  22.  
  23. struct tmz {
  24.     char *name;
  25.     char *dstname;
  26.     int minwest;
  27. } zones[] = {
  28.     { "GMT", "GMT",       0*60 },
  29.     { "AST", "ADT",       4*60 },
  30.     { "EST", "EDT",       5*60 },
  31.     { "CST", "CDT",       6*60 },
  32.     { "MST", "MDT",       7*60 },
  33.     { "PST", "PDT",       8*60 },
  34.     { "YST", "YDT",       9*60 },
  35.     { "HST", "HDT",      10*60 },
  36.     { "BST", "BDT",      11*60 },
  37.     { "WET", "WET DST",   0*60 },
  38.     { "MET", "MET DST",  -1*60 },
  39.     { "EET", "EET DST",  -2*60 },
  40.     { "EST", "EST",     -10*60 },
  41.     { "CST", "CST", -(9*60+30) },
  42.     { "WST", "WST",      -8*60 },
  43. };
  44.  
  45. lookupzone(z) {
  46.     int i;
  47.     for(i = 0; i < sizeof(zones)/sizeof(struct tmz); i++) {
  48.     if (ustrcmp(z,zones[i].name) == 0)
  49.         return(zones[i].minwest);
  50.     if (ustrcmp(z,zones[i].dstname) == 0)
  51.         return(zones[i].minwest-60);
  52.     }
  53.     return(-1);
  54. }
  55.  
  56. mbox_date(str) 
  57. char *str;
  58. {
  59.     int n;
  60.     int t,i;
  61.     char mon[4],dow[4];
  62.     int day, month, year, hr, min, sec,isaleap;
  63.     
  64.     n = sscanf(str, "%3s %3s %2d %2d:%2d:%2d %4d\n",
  65.            dow,mon,&day,&hr,&min,&sec,&year);
  66.     if (n != 7)
  67.     return(stringtotime(str));
  68.     if ((month = whatmonth(mon)) == -1)
  69.     return(stringtotime(str));
  70.     t = mkdate(year,month,day,hr,min,sec,NULL);
  71.     return(t);
  72. }
  73.  
  74. mtxt_date(str) 
  75. char *str;
  76. {
  77.     int n;
  78.     int t,i;
  79.     char mon[4],tz[10];
  80.     int day, month, year, hr, min, sec,isaleap;
  81.     
  82.     n = sscanf(str, "%2d-%3s-%d %2d:%2d:%d-%3s\n",&day,mon,&year,&hr,&min,
  83.            &sec,tz);
  84.     if (n != 7)
  85.     return(stringtotime(str));
  86.     if ((month = whatmonth(mon)) == -1)
  87.     return(stringtotime(str));
  88.     if (year < 100) year += 1900;
  89.     t = mkdate(year,month,day,hr,min,sec,tz);
  90.     return(t);
  91. }
  92.  
  93. babyl_date(str) 
  94. char *str;
  95. {
  96.     int n;
  97.     int t,i;
  98.     char mon[4],tz[4],dow[4];
  99.     int day, month, year, hr, min, sec,isaleap;
  100.     int howmany = 8;
  101.  
  102.     n = sscanf(str, "%3s, %2d %3s %d %2d:%2d:%2d %3s\n",
  103.            dow,&day,mon,&year,&hr,&min,&sec,tz);
  104.     if (n == 8) {
  105.     if ((month = whatmonth(mon)) == -1)
  106.         return(stringtotime(str));
  107.     if (year < 100) year += 1900;
  108.     return(mkdate(year,month,day,hr,min,sec,tz));
  109.     }
  110.     n = sscanf(str, "%3s, %2d %3s %d, %2d:%2d:%2d %3s\n",
  111.            dow,&day,mon,&year,&hr,&min,&sec,tz);
  112.     if (n == 8) {
  113.     if ((month = whatmonth(mon)) == -1)
  114.         return(stringtotime(str));
  115.     if (year < 100) year += 1900;
  116.     return(mkdate(year,month,day,hr,min,sec,tz));
  117.     }
  118.     n = sscanf(str, "%3s %2d %3s %d %2d:%2d:%2d %3s\n",
  119.            dow,&day,mon,&year,&hr,&min,&sec,tz);
  120.     if (n == 8)  {
  121.     if ((month = whatmonth(mon)) == -1)
  122.         return(stringtotime(str));
  123.     if (year < 100) year += 1900;
  124.     return(mkdate(year,month,day,hr,min,sec,tz));
  125.     }
  126.     n = sscanf(str, "%3s, %2d %3s %d %2d:%2d %3s\n",
  127.            dow,&day,mon,&year,&hr,&min,tz);
  128.     if (n == 7) {
  129.     if ((month = whatmonth(mon)) == -1)
  130.         return(stringtotime(str));
  131.     if (year < 100) year += 1900;
  132.     return(mkdate(year,month,day,hr,min,0,tz));
  133.     }
  134.     n = sscanf(str, "%3s %2d %3s %d %2d:%2d %3s\n",
  135.            dow,&day,mon,&year,&hr,&min,tz);
  136.     if (n == 7) {
  137.     if ((month = whatmonth(mon)) == -1)
  138.         return(stringtotime(str));
  139.     if (year < 100) year += 1900;
  140.     return(mkdate(year,month,day,hr,min,0,tz));
  141.     }
  142.     return(stringtotime(str));
  143. }
  144.  
  145. whatmonth(mon)
  146. char *mon;
  147. {
  148.     int i;
  149.     for(i = 0; i < 12; i++) {
  150.     if (strcmp(months[i], mon) == 0) {
  151.         return(i);
  152.     }
  153.     }
  154.     return(-1);
  155. }
  156.  
  157. mkdate(yr,mon,day,hr,min,sec,z)
  158. int yr,mon,day,hr,min,sec;
  159. char *z;
  160. {
  161.     int t,i;
  162.     int isaleap;
  163.     datime dt;
  164.     struct tm *tm;
  165. #ifdef BSD
  166.     struct timeval tp;
  167.     struct timezone tz;
  168. #endif
  169.     int zone;
  170.  
  171.     if (z) 
  172.     zone = lookupzone(z);
  173.     else
  174.     zone = -1;
  175. #ifdef BSD
  176.     gettimeofday(&tp, &tz);
  177. #endif
  178.     dt._dtsec = sec;
  179.     dt._dtmin = min;
  180.     dt._dthr = hr;
  181.     dt._dtday = day-1;
  182.     dt._dtmon = mon;
  183.     dt._dtyr = yr;
  184.     dt._dtdst = 0;
  185.     dt._dttz = 0;
  186.     t = datime_to_time(&dt);
  187.     tm = localtime(&t);
  188. #if SYSV
  189.     asctime(tm);
  190.     t += (zone != -1) ? zone*60 : timezone;
  191. #endif
  192. #if BSD
  193.     t += (zone != -1) ? zone*60 : tz.tz_minuteswest*60;
  194. #endif
  195.     if ((zone == -1) && (tm->tm_isdst)) t -= 3600;
  196.     return(t);
  197. }
  198.  
  199. #ifdef undef
  200. main() {
  201.     mbox_date("Jan  1 00:00:00 1970");
  202.     mtxt_date("01-Jan-70 00:00:00-GMT");
  203.     babyl_date("Thu, 1 Jan 70 00:00:00 EST");
  204.     mbox_date("Jun  1 00:00:00 1970");    /* some IN dst */
  205.     mtxt_date("01-Jun-70 00:00:00-GMT");
  206.     babyl_date("Thu, 1 Jun 70 00:00:00 EST");
  207. }
  208.  
  209. #endif
  210.